/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.generics.client.view;

import br.com.sales.generics.client.components.scroll.HScroll;
import br.com.sales.generics.client.presenter.MyMainLayoutPresenter;
import br.com.sales.generics.client.presenter.MyMainLayoutUiHandler;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;

/**
 *
 * @author v
 */
public class MyMainLayoutView
        extends ViewWithUiHandlers<MyMainLayoutUiHandler>
        implements MyMainLayoutPresenter.Display {

    private static final HanForPIViewUiBinder uiBinder = GWT.create(HanForPIViewUiBinder.class);
    private final HScroll mainContentScrollPanel = new HScroll();
    @UiField
    LayoutPanel mainContentPanel;
    @UiField
    VerticalPanel mainLayoutPanel;

    @Inject
    public MyMainLayoutView() {
        mainLayoutPanel = uiBinder.createAndBindUi(this);
    }

    @Override
    public Widget asWidget() {
        return mainLayoutPanel;
    }

    @UiHandler("btnHistory")
    public void onClickHistory(ClickEvent event) {
        getUiHandlers().onClickHistory();
    }

    @UiHandler("btnSales")
    public void onClickNewSale(ClickEvent event) {
        getUiHandlers().onClickNewSale();
    }

    @UiHandler("btnCadastre")
    public void onClickCadastre(ClickEvent event) {
        getUiHandlers().onCadastreMouseEvent(true);
    }
    
    @UiHandler("logout")
    public void onClickLogout(ClickEvent event) {
        getUiHandlers().onLogoutClick(event);
    }

    private void setMainContent(IsWidget content) {
        mainContentPanel.clear();
        mainContentScrollPanel.clear();
        if (content != null) {
            mainContentPanel.remove(mainContentScrollPanel);
            mainContentPanel.add(content);
        }
    }

    private void setMainContentScroll(IsWidget content) {
        mainContentScrollPanel.clear();
        mainContentPanel.clear();

        if (content != null) {
            mainContentScrollPanel.add(content);
            mainContentPanel.add(mainContentScrollPanel);
        }
    }

    @Override
    public void setInSlot(Object slot, IsWidget content) {
        if (slot == MyMainLayoutPresenter.CONTENT_SLOT_TYPE) {
            if (MyMainLayoutPresenter.SCROLL_PLACE) {
                setMainContentScroll(content);
            } else {
                setMainContent(content);
            }
        } else {
            super.setInSlot(slot, content);
        }
    }

    interface HanForPIViewUiBinder extends UiBinder<VerticalPanel, MyMainLayoutView> {
    }
}
